/**
 * Created by zhangqingxi on 2017/3/15.
 */


$(function () {
    var processId=$("#processId").val();
    if(processId!=null&&processId!=""){
        //设置业务标识为不可编辑
        $("#businessIdentity").attr("disabled","disabled");
        $.ajax({
            type: "POST",
            url: ROOT_PATH + '/workflow/workflowDef/queryWorkflowDefInfoByProcessId.shtml',
            data: {processId: processId},
            success: function (result) {
                result = $.parseJSON(result);
                initWorkflowInfo(result);
            }
        });
    }


    /***
     * 移除环节事件
     */
   /* $(".delate").on('click',function(){
        $(this).parent().remove();
    });*/
    /***
     * 添加环节事件
     */
    $(".append").on('click',function(){
        createActivityRow(null);
    });

    /***
     * 提交操作
     */
    $('#workflowDefSave').on("click", function () {
        $.messager.progress();	// 显示进度条
        var workflowDefInfo = getFlowDefInfo();//流程信息

        $('#workflowDefForm').form('submit', {
            url: ROOT_PATH + '/workflow/workflowDef/addWorkflowDef.shtml',
            queryParams:{
                workflowDefInfo:workflowDefInfo
            },
            onSubmit: function (param) {
                return true;	// 返回false终止表单提交
            },
            success: function (result) {
                try {
                    result = $.parseJSON(result);
                }catch (e) {
                    $.messager.progress('close');
                    parent.layer.msg(result.message,{time:1000,icon:2});
                }
                if (result.success){
                    $.messager.progress('close');	// 如果提交成功则隐藏进度条
                    parent.layer.msg(result.message,{time:1000,icon:1},function () {
                        parent.$('#dg1').datagrid('load');
                        parent.layer.closeAll(); //先得到当前iframe层的索引
                    });
                }else {
                    $.messager.progress('close');	// 如果提交失败则不做处理
                    parent.layer.msg(result.message,{time:1000,icon:2});
                }
            }
        });
    });

    /***
     * 信息验证
     */
    $.extend($.fn.validatebox.defaults.rules, {
        //验证业务标识唯一性
        checkBusinessIdentity : {
            validator: function (value) {
                //alert(value);
                var checkR=$.ajax({
                    async : false,
                    cache : false,
                    type : 'post',
                    url : ROOT_PATH+'/workflow/flowBusiness/checkBusinessIdentity.shtml',
                    data : {
                        identity:value
                    }
                }).responseText;
                var dataObj=$.parseJSON(checkR);//转换为json对象
                var flag=dataObj.success==true;
                if(!flag){
                    $('#identity_err').css('display','block');
                }else {
                    $('#identity_err').css('display','none');
                }
                return flag;
            },
            message: '当前业务标识已存在！'
        }
    });

});


/***
 * 展开隐藏详细内容事件
 */
function showActivityContents(node) {
    var childs=$(node).parent().parent().children();
    var contentsDiv=$(childs[childs.length-1]);
    if($(contentsDiv).is(":hidden")){
        $(contentsDiv).show();
    }else{
        $(contentsDiv).hide();
    }
};
/***
 * 初始化流程定义信息
 * @param workflowDefInfo
 */
function initWorkflowInfo(workflowDefInfo){
    //流程名称
    var workflowName=workflowDefInfo.workflowName;
    $("#workflowName").textbox('setValue',workflowName);

    var activitys=workflowDefInfo.activitys;//流程环节
    var workflowDefInfoDiv=$("#workflowDefInfo");
    var activityRows=workflowDefInfoDiv.children(".definitionBox");

    for(var i=0;i<activitys.length;i++){
        var activity=activitys[i];
        var activityRow=activityRows[i];
        if(i==0){//开始节点
            $(activityRow).find("input[name='activityId']").val(activity.activityId);//环节id
            $(activityRow).find("input[name='activityName']").val(activity.activityName);//环节名称
            $(activityRow).find("select[name='activityType']").val(activity.activityType);//环节类型
        }else if(i==1){//activity节点
            $(activityRow).find("input[name='activityId']").val(activity.activityId);//环节id
            $(activityRow).find("input[name='activityName']").val(activity.activityName);//环节名称
            $(activityRow).find("select[name='activityType']").val(activity.activityType);//环节类型
            $(activityRow).find("input[name='editForm']").val(activity.editForm);//编辑表单
            $(activityRow).find("input[name='viewForm']").val(activity.viewForm);//只读表单
            //是否动态指定
            if(activity.isDynamicAssigner!=null&&1==activity.isDynamicAssigner){
                $(activityRow).find("input[name='isDynamicAssigner']").attr("checked",true);
            }
        }else if(i==activitys.length-1){//结束节点
            $(activityRows[activityRows.length-1]).find("input[name='activityId']").val(activity.activityId);//环节id
            $(activityRows[activityRows.length-1]).find("input[name='activityName']").val(activity.activityName);//环节名称
            $(activityRows[activityRows.length-1]).find("select[name='activityType']").val(activity.activityType);//环节类型
        }else if(i>=activityRows.length-1){//中间流程环节
            createActivityRow(activity);//创建节点信息
        }
    }
    //开始节点信息

}
/***
 * 创建流程节点
 */
function createActivityRow(activityObject){
    //环节div
    var activiDiv=$("<div>");
    activiDiv.addClass("definitionBox");
    //标题div
    var rowDiv=$("<div>");
    rowDiv.addClass("definitionBox-one");
    activiDiv.append(rowDiv);
    //加号
    var jiaDiv=$("<a>");
    jiaDiv.attr("name","clickA");
    jiaDiv.text("+");
    jiaDiv.on('click',function () {//展开隐藏事件
        showActivityContents(this);
    });
    rowDiv.append(jiaDiv);
    //标题-环节名称
    var nameDiv=$("<div>");
    rowDiv.append(nameDiv);
    nameDiv.addClass("input-one");
    var nameLabel=$("<label>");
    nameLabel.addClass("definitionA");
    nameLabel.text("环节名称 :");
    nameDiv.append(nameLabel);
    var idInput=$("<input>");
    idInput.attr("type","hidden");
    idInput.attr("name","activityType");
    idInput.val(activityObject!=null?activityObject.activityId:'');
    nameDiv.append(idInput);
    var nameInput=$("<input>");
    nameInput.addClass("easyui-textbox nameA");
    nameInput.attr("type","text");
    nameInput.attr("name","activityName");
    nameInput.val(activityObject!=null?activityObject.activityName:'');
    nameDiv.append(nameInput);
    //标题-环节类型
    var typeDiv=$("<div>");
    rowDiv.append(typeDiv);
    typeDiv.addClass("input-one");
    var typeLabel=$("<label>");
    typeLabel.addClass("definitionA");
    typeLabel.text("环节类型 :");
    typeDiv.append(typeLabel);
    var typeInput=$("<select>");
    typeInput.addClass("nameA");
    typeInput.attr("data-options","required:true,editable:false,selectOnNavigation:true");
    typeInput.attr("name","activityType");

    var option=$("<option>");
    option.attr("value","2");
    option.text("任务节点");
    typeInput.append(option);
    typeDiv.append(typeInput);
    typeInput.val(activityObject!=null?activityObject.activityType:2);
    //删除按钮
    var delDiv=$("<div>");
    delDiv.addClass("delate");
    delDiv.on('click',function(){//移除事件
        $(this).parent().remove();
    });
    activiDiv.append(delDiv);
    var delA= $("<a>");
    delA.text("-");
    delDiv.append(delA);

    //详细参数内容div
    var contentsDiv=$("<div>");
    contentsDiv.addClass("fold");
    activiDiv.append(contentsDiv);
    //编辑表单
    var editDiv=$("<div>");
    var editFormInput=$("<input>");
    editFormInput.attr('type','text');
    editFormInput.attr('name','editForm');
    editFormInput.addClass("fold_input");
    //debugger
    editFormInput.val(activityObject!=null?activityObject.editForm:'');
    editDiv.append("编辑表单：");
    editDiv.append(editFormInput);
    contentsDiv.append(editDiv);
    //只读表单
    var viewDiv=$("<div>");
    var viewFormInput=$("<input>");
    viewFormInput.attr('type','text');
    viewFormInput.attr('name','viewForm');
    viewFormInput.addClass("fold_input");
    viewFormInput.val(activityObject!=null?activityObject.viewForm:'');
    viewDiv.append("只读表单：");
    viewDiv.append(viewFormInput);
    contentsDiv.append(viewDiv);
    //是否动态指定
    var dynamicDiv=$("<div>");
    var dynamicLabel=$("<label>");
    var dynamicInput=$("<input>");
    dynamicLabel.append(dynamicLabel);
    dynamicInput.attr('type','checkbox');
    dynamicInput.attr('name','isDynamicAssigner');
    dynamicInput.attr("value","1");
    //dynamicInput.val(activityObject!=null?activityObject.viewForm:'');
    dynamicLabel.append(dynamicInput);
    dynamicLabel.append("是否由上一环节动态指定");
    dynamicDiv.append(dynamicLabel);
    contentsDiv.append(dynamicDiv);
    //debugger
    if(activityObject!=null&&activityObject.isDynamicAssigner!=null&&1==activityObject.isDynamicAssigner){
        dynamicInput.attr("checked",true);
    }

    //添加到页面
    var childActivitys=$("#workflowDefInfo").children("div");
    $(childActivitys[childActivitys.length-2]).after(activiDiv);
}
/***
 * 获取流程定义数据
 */
function getFlowDefInfo() {
    //业务标识
    var businessIdentity = $("#businessIdentity").val();
    alert(businessIdentity);

    //流程节点信息
    var workflowDefInfoDiv=$("#workflowDefInfo");
    var activityRows=workflowDefInfoDiv.children(".definitionBox");
    var defInfoJson=[];
    for(var i=0;i<activityRows.length;i++){
        var activityInfo={};
        var activity=activityRows[i];
        var activityId= $(activity).find("input[name='activityId']").val();//环节id
       var activityName= $(activity).find("input[name='activityName']").val();//环节名称
       var activityType= $(activity).find("select[name='activityType']").val();//环节类型
        var editForm= $(activity).find("input[name='editForm']").val();//环编辑表单
        var viewForm= $(activity).find("input[name='viewForm']").val();//只读表单
        var isDynamicAssigner=0;
        if($(activity).find("input[name='isDynamicAssigner']").is(':checked')){//是否由上一环节动态指定
            isDynamicAssigner=1;
        };
        activityInfo.activityId=activityId;
        activityInfo.activityName=activityName;
        activityInfo.activityType=activityType;
        activityInfo.editForm=editForm;
        activityInfo.viewForm=viewForm;
        activityInfo.isDynamicAssigner=isDynamicAssigner;
        defInfoJson.push(activityInfo);
    }
    //流程id及流程名称
    var processId=$("input[name='processId']").val();
    var workflowName=$("input[name='workflowName']").val();
    var workflowInfo={};
    workflowInfo.processId=processId;
    workflowInfo.workflowName=workflowName;
    workflowInfo.businessIdentity=businessIdentity;//业务标识
    workflowInfo.activitys=defInfoJson;

    return JSON.stringify(workflowInfo);
}




